package edu.scau.cxq.gp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.common.utils.WrapperUtils;
import edu.scau.cxq.gp.entity.dto.GoodsSignDTO;
import edu.scau.cxq.gp.entity.Goods;
import edu.scau.cxq.gp.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 存储货物信息 前端控制器
 * @return
 * @author TLC
 */


@RestController
@ResponseResult
@RequestMapping("/goods")
public class GoodsController {
    
    @Resource
    private IGoodsService goodsService;

    /**
     * 插入新货物
     * @param goods
     * @return
     */
    @PostMapping("/insertOne")
    public boolean insertOne(@RequestBody Goods goods) {
        return goodsService.save(goods);
    }

    /**
     * 查找货物
     * @return
     */
    @GetMapping("/select")
    public List<Goods> select(Goods goods) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(goods);
        queryWrapper.eq("goods_hidden", 0);
//        select * from goods where goods_hidden=?
        List<Goods> goodss = goodsService.list(queryWrapper);
        return goodss;
    }

    /**
     * 查找单个货物
     */
    @GetMapping("/selectOne")
    public Goods selectOne (Long goodsId) {

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);
//        select goods from goods where goods_id=?
        Goods goods = goodsService.getOne(queryWrapper);

        return goods;
    }

    /**
     * 分页查找货物
     * @return
     */
    @GetMapping("/selectByPage")
    public IPage<Goods> selectByPage (Integer currentPage, Integer size, Goods goods) {
        //创建分页对象
        Page<Goods> page = new Page<>(currentPage, size);
        //构建条件构造器
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(goods);
        queryWrapper.eq("goods_hidden", 0);
        //返回分页查询结果
        return goodsService.page(page, queryWrapper);
    }

    /**
     * 删除（实际上是隐藏）
     * @param goods
     * @return
     */
    @PostMapping("/deleteOne")
    public boolean deleteOne(@RequestBody Goods goods) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(goods);
        goods = goodsService.getOne(queryWrapper);
        // 将隐藏位置1 set（设置）
        goods.setGoodsHidden(1);
        return goodsService.updateById(goods);
    }

    /**
     * 修改货物
     * @RequestBody：
     * 作用：
     * 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     * @param goods
     * @return
     */
    @PostMapping("/updateOne")
    public boolean updateOne (@RequestBody Goods goods) {

        return goodsService.updateById(goods);
    }

    /**
     * 获取所有的货物标签
     * @return
     */
    @GetMapping("/selectGoodsSigns")
    public List<GoodsSignDTO> selectGoodsSigns () {

        return goodsService.findGoodsSigns();
    }
}
